﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.Plumbing.LayReliefOpen;

namespace YArchitech.Plumbing
{
	public partial class FormLayReliefOpening : HYBaseForm
	{
		public FormLayReliefOpening()
		{
			this.InitializeComponent();
		}

		public FormLayReliefOpening(float area) : this()
		{
			if (area != 0f)
			{
				this.inputArea = true;
				this.area = area;
			}
		}

		private void FormLayReliefOpening_Load(object sender, EventArgs e)
		{
			this.imgAuto = Path.Combine(Product.FamilyImageLocation, "ReliefOpening", "自动泄压阀.jpg");
			this.imgMechanical = Path.Combine(Product.FamilyImageLocation, "ReliefOpening", "机械式开启泄压阀.jpg");
			this.userSetting = this.GetUserData();
			this.allData = this.ReadDbData();
			this.tbArea.Text = this.userSetting.AreaSize.ToString("f3");
			this.tbNumber.Text = this.userSetting.Number.ToString();
			this.cbLinked.Checked = this.userSetting.IsLinkedDocument;
			if (this.userSetting.IsAutoSelection)
			{
				this.rbAutoSelect.Checked = true;
				return;
			}
			this.rbAllModel.Checked = true;
		}

		private void btnLay_Click(object sender, EventArgs e)
		{
			if (this.dgvDataList.SelectedRows.Count <= 0)
			{
				YJKMessageBox.Show("请选中要布置的类型行！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				return;
			}
			this.SelectedItem.Model = this.dgvDataList.SelectedRows[0].Cells["ColModel"].Value.ToString();
			this.SelectedItem.TypeInfo = this.dgvDataList.SelectedRows[0].Cells["ColType"].Value.ToString();
			this.SelectedItem.Width = Convert.ToInt32(this.dgvDataList.SelectedRows[0].Cells["ColWidth"].Value);
			this.SelectedItem.Height = Convert.ToInt32(this.dgvDataList.SelectedRows[0].Cells["ColHeight"].Value);
			this.SelectedItem.Area = this.dgvDataList.SelectedRows[0].Cells["ColArea"].Value.ToString();
			this.SelectedItem.OpeningSize = this.dgvDataList.SelectedRows[0].Cells["ColOpeningSize"].Value.ToString();
			this.SelectedItem.Company = this.dgvDataList.SelectedRows[0].Cells["ColCompany"].Value.ToString();
			this.SelectedItem.OpeningWidth = this.Transfer(this.SelectedItem.OpeningSize, true);
			this.SelectedItem.OpeningHeight = this.Transfer(this.SelectedItem.OpeningSize, false);
			base.DialogResult = DialogResult.OK;
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Cancel;
		}

		private void rbAutoSelect_CheckedChanged(object sender, EventArgs e)
		{
			RadioButton radioButton = sender as RadioButton;
			if (!radioButton.Checked)
			{
				return;
			}
			DataTable dtData;
			if (radioButton.Name == "rbAutoSelect")
			{
				this.tbArea.Enabled = true;
				this.tbNumber.Enabled = true;
				dtData = this.FilterData();
			}
			else
			{
				this.tbArea.Enabled = false;
				this.tbNumber.Enabled = false;
				dtData = this.allData.Copy();
			}
			this.RefreshData(dtData);
		}

		private void RefreshData(DataTable dtData)
		{
			if (this.dgvDataList.DataSource != null)
			{
				DataTable dataTable = (DataTable)this.dgvDataList.DataSource;
				dataTable.Rows.Clear();
				this.dgvDataList.DataSource = dataTable;
			}
			else
			{
				this.dgvDataList.Rows.Clear();
			}
			if (dtData == null || dtData.Rows.Count <= 0)
			{
				return;
			}
			this.dgvDataList.DataSource = dtData;
			bool flag = false;
			if (!string.IsNullOrEmpty(this.userSetting.Model) && !string.IsNullOrEmpty(this.userSetting.Company))
			{
				this.dgvDataList.ClearSelection();
				foreach (object obj in ((IEnumerable)this.dgvDataList.Rows))
				{
					DataGridViewRow dataGridViewRow = (DataGridViewRow)obj;
					if (dataGridViewRow.Cells["ColModel"].Value.ToString() == this.userSetting.Model && dataGridViewRow.Cells["ColCompany"].Value.ToString() == this.userSetting.Company)
					{
						flag = true;
						dataGridViewRow.Selected = true;
						this.dgvDataList.CurrentCell = dataGridViewRow.Cells["ColModel"];
					}
				}
				if (!flag && this.dgvDataList.Rows.Count > 0)
				{
					this.dgvDataList.Rows[0].Selected = true;
					this.dgvDataList.CurrentCell = this.dgvDataList.Rows[0].Cells["ColModel"];
					return;
				}
			}
			else if (this.dgvDataList.Rows.Count > 0)
			{
				this.dgvDataList.Rows[0].Selected = true;
				this.dgvDataList.CurrentCell = this.dgvDataList.Rows[0].Cells["ColModel"];
			}
		}

		private void RefreshData(IList<ReliefOpening> dataList)
		{
			this.dgvDataList.Rows.Clear();
			if (dataList == null || dataList.Count <= 0)
			{
				return;
			}
			this.dgvDataList.DataSource = dataList;
		}

		private DataTable ReadDbData()
		{
			SQLiteDataOperate.SetDataSource(Path.Combine(Product.DataLocation, "HYBIMSPACE.db3"));
			return SQLiteDataOperate.ExecQuery(string.Format(" select * from ReliefOpening; ", Array.Empty<object>()));
		}

		private void tbArea_Leave(object sender, EventArgs e)
		{
			System.Windows.Forms.TextBox textBox = sender as System.Windows.Forms.TextBox;
			if (string.IsNullOrEmpty(textBox.Text.Trim()))
			{
				string arg = (textBox.Name == "tbArea") ? "泄压口面积" : "泄压口数量";
				YJKMessageBox.Show(string.Format("{0}值不能为空.", arg), MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				textBox.Focus();
				return;
			}
			if (textBox.Name == "tbArea")
			{
				if (!float.TryParse(this.tbArea.Text, out this.area))
				{
					YJKMessageBox.Show("泄压口面积输入值格式有误！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
					this.tbArea.SelectAll();
					this.tbArea.Focus();
					return;
				}
				this.tbArea.Text = this.area.ToString("f3");
			}
			else if (this.tbNumber.Text.Trim() == "0")
			{
				YJKMessageBox.Show("泄压口数量不能为0！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				this.tbNumber.SelectAll();
				this.tbNumber.Focus();
				return;
			}
			DataTable dtData = this.FilterData();
			this.RefreshData(dtData);
		}

		private void tbArea_KeyPress(object sender, KeyPressEventArgs e)
		{
			if ((e.KeyChar < '0' || e.KeyChar > '9') && e.KeyChar != '\b' && e.KeyChar != '.' && e.KeyChar != '\r')
			{
				e.Handled = true;
				return;
			}
			if (e.KeyChar == '\r')
			{
				this.tbNumber.Focus();
			}
			if (e.KeyChar == '.' && ((System.Windows.Forms.TextBox)sender).Text.IndexOf('.') != -1)
			{
				e.Handled = true;
				return;
			}
			if (e.KeyChar == '.' && ((System.Windows.Forms.TextBox)sender).Text == "")
			{
				e.Handled = true;
				return;
			}
			if (e.KeyChar != '.' && ((System.Windows.Forms.TextBox)sender).Text == "0")
			{
				e.Handled = true;
				return;
			}
		}

		private void tbNumber_KeyPress(object sender, KeyPressEventArgs e)
		{
			if ((e.KeyChar < '0' || e.KeyChar > '9') && e.KeyChar != '\b' && e.KeyChar != '\r')
			{
				e.Handled = true;
				return;
			}
			if (e.KeyChar == '\r')
			{
				this.dgvDataList.Focus();
			}
		}

		private void dgvDataList_SelectionChanged(object sender, EventArgs e)
		{
			if (this.dgvDataList.SelectedRows.Count <= 0)
			{
				return;
			}
			string a = this.dgvDataList.SelectedRows[0].Cells["ColType"].Value.ToString();
			if (!(a == "自动式"))
			{
				if (!(a == "机械式"))
				{
					this.pbPreview.Image = null;
					return;
				}
				if (File.Exists(this.imgMechanical))
				{
					this.pbPreview.Image = new Bitmap(this.imgMechanical);
					return;
				}
				this.pbPreview.Image = null;
				return;
			}
			else
			{
				if (File.Exists(this.imgAuto))
				{
					this.pbPreview.Image = new Bitmap(this.imgAuto);
					return;
				}
				this.pbPreview.Image = null;
				return;
			}
		}

		private void cbLinked_CheckedChanged(object sender, EventArgs e)
		{
			this.LinkChecked = this.cbLinked.Checked;
		}

		private DataTable FilterData()
		{
			if (this.allData == null)
			{
				return null;
			}
			DataTable result = null;
			float num = float.Parse(this.tbArea.Text.Trim());
			int num2 = Convert.ToInt32(this.tbNumber.Text.Trim());
			float num3 = num / (float)num2;
			DataRow[] source = this.allData.Select("MaxArea>=" + num3, "MaxArea ASC");
			if (source.Count<DataRow>() > 0)
			{
				result = source.CopyToDataTable<DataRow>();
			}
			return result;
		}

		private double Transfer(string openingSize, bool isWidth)
		{
			double result = 0.0;
			if (string.IsNullOrEmpty(openingSize))
			{
				return result;
			}
			string[] array = openingSize.Split(new char[]
			{
				'X'
			});
			if (isWidth)
			{
				return Convert.ToDouble(array[0]);
			}
			return Convert.ToDouble(array[1]);
		}

		private ReliefSetting GetUserData()
		{
			ReliefSetting reliefSetting = new ReliefSetting();
			string[] array = this.userSettingMgr.ReadReliefInfo();
			if (array[0] == "")
			{
				reliefSetting.IsAutoSelection = true;
			}
			else
			{
				reliefSetting.IsAutoSelection = !(array[0] == "0");
			}
			if (this.inputArea)
			{
				reliefSetting.AreaSize = this.area;
			}
			else
			{
				reliefSetting.AreaSize = ((array[1] == "") ? this.area : float.Parse(array[1]));
			}
			reliefSetting.Number = ((array[2] == "") ? this.number : int.Parse(array[2]));
			reliefSetting.Model = ((array[3] == "") ? "" : array[3]);
			reliefSetting.Company = ((array[4] == "") ? "" : array[4]);
			if (array[5] == "")
			{
				reliefSetting.IsLinkedDocument = false;
			}
			else
			{
				reliefSetting.IsLinkedDocument = !(array[5] == "0");
			}
			return reliefSetting;
		}

		private void FormLayReliefOpening_FormClosing(object sender, FormClosingEventArgs e)
		{
			string[] array = new string[6];
			array[0] = (this.rbAutoSelect.Checked ? "1" : "0");
			array[1] = this.tbArea.Text.Trim();
			array[2] = this.tbNumber.Text.Trim();
			if (this.dgvDataList.Rows.Count > 0)
			{
				DataGridViewRow dataGridViewRow = this.dgvDataList.SelectedRows[0];
				if (dataGridViewRow != null)
				{
					array[3] = dataGridViewRow.Cells["ColModel"].Value.ToString();
					array[4] = dataGridViewRow.Cells["ColCompany"].Value.ToString();
				}
			}
			array[5] = (this.cbLinked.Checked ? "1" : "0");
			this.userSettingMgr.WriteReliefInfo(array);
		}

		private float area = 0.21f;

		private int number = 1;

		private bool inputArea;

		private DataTable allData;

		private string imgAuto;

		private string imgMechanical;

		private ReliefSetting userSetting;

		private HVACUserSetting userSettingMgr = new HVACUserSetting();

		public ReliefOpening SelectedItem = new ReliefOpening();

		public bool LinkChecked;
	}
}
